package com.shd.edanpei.presenter;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.dialog.DialogUtil;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.bean.CustomerBean;
import com.shd.edanpei.bean.StatusBean;
import com.shd.edanpei.presenter.contract.CustomerInfoContract;
import com.shd.edanpei.util.GsonUtil;
import com.shd.edanpei.util.ToastUtil;
import com.shd.edanpei.util.Validator;

import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/9/22.
 * yang-wen-wen
 */

public class CustomerInfoPresenter extends BasePresenterImpl<CustomerInfoContract.CustomerInfoIView> implements CustomerInfoContract.CustomerInfoIPresenter {

    private CustomerBean bean = new CustomerBean();
    //提交按钮是否可点击
    private boolean canSubmit = false;

    public CustomerInfoPresenter(CustomerInfoContract.CustomerInfoIView mView) {
        super(mView);
    }

    @Override
    public void setCustomerBean(CustomerBean customerBean) {
        this.bean = customerBean;
        mBaseIView.setData(bean);
        checkBtn();
    }

    @Override
    public void getCustomerInfo(Bundle bundle) {
        ApiCreator.getApi().getCustomerInfo(bundle.getInt(Constant.ORDER_ID))
                .compose(RxTransformer.<CustomerBean>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<CustomerBean>() {
                    @Override
                    protected void onSuccess(CustomerBean data) {
                        bean = data;
                        mBaseIView.setData(data);
                    }
                });
    }

    @Override
    public void setName(String str) {
        bean.setName(str);
        checkBtn();
    }

    public void setGender(int gender) {
        bean.setSex(gender + "");
    }

    @Override
    public void setBirthday(final TextView text) {
        DialogUtil.dateDialog(mBaseIView.getContext(), "出生日期", new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                mBaseIView.setText(text, data);
                bean.setBirthday(data);
                checkBtn();
            }
        });
    }

    public void setBirthday(String str) {
        bean.setBirthday(str);
    }

    @Override
    public void setIdNumber(String str) {
        bean.setIdCardNo(str);
        checkBtn();
    }

    @Override
    public void setContact(String str) {
        bean.setMobile(str);
        checkBtn();
    }

    @Override
    public void setSpouseName(String str) {
        bean.setSpouseName(str);
        checkBtn();
    }

    @Override
    public void setSpouseContact(String str) {
        bean.setSpouseMobile(str);
        checkBtn();
    }

    @Override
    public void setMarital(boolean b) {
        bean.setMarrage(b);
        checkBtn();
    }

    @Override
    public void setSalary(final TextView text) {
        DialogUtil.listDialog(mBaseIView.getContext(), "当前收入", R.array.salary,
                new DialogUtil.Listener() {
                    @Override
                    public void done(String data) {
                        mBaseIView.setText(text, data);
                        bean.setIncome(data);
                        checkBtn();
                    }
                });
    }

    @Override
    public void setIndustry(final TextView text) {
        DialogUtil.listDialog(mBaseIView.getContext(), "行业", R.array.industry,
                new DialogUtil.Listener() {
                    @Override
                    public void done(String data) {
                        mBaseIView.setText(text, data);
                        bean.setIndustry(data);
                        checkBtn();
                    }
                });
    }

    @Override
    public void setLiveAddress(final TextView text) {
        DialogUtil.addressPick(mBaseIView.getContext(), new DialogUtil.AddressListener() {
            @Override
            public void done(String address, String provinceCode, String cityCode, String districtCode) {
                mBaseIView.setText(text, address);
                bean.setAddress(address);
                checkBtn();
            }
        });
    }

    @Override
    public void setDetailAddress(String str) {
        bean.setDetailAddress(str);
        checkBtn();
    }

    @Override
    public void submit() {
        if (canSubmit) {
            Map<String, Object> map = GsonUtil.toMap(bean);
            ApiCreator.getApi().submitCustomerInfo(map)
                    .compose(RxTransformer.<Integer>errorHandle())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<Integer>() {
                        @Override
                        protected void onSuccess(Integer data) {
                            ToastUtil.showToast("提交成功");
                            bean.setCustomerId(data + "");
                            RxBus.get().send(RxBusCode.CUSTOMER_INFO_SUBMIT_SUCCESS, bean);
                            mBaseIView.getContext().finish();
                        }
                    });
        }
    }

    @Override
    public void updata(final int index) {
        if (canSubmit) {
            Map<String, Object> map = GsonUtil.toMap(bean);
            ApiCreator.getApi().updateCustomerInfo(map)
                    .compose(RxTransformer.<NoDataResponse>errorHandle())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<NoDataResponse>() {
                        @Override
                        protected void onSuccess(NoDataResponse data) {
                            RxBus.get().send(RxBusCode.UPDATE_CUSTOMER_INFO_SUCCESS, new StatusBean(index, bean.getName()));
                            RxBus.get().send(RxBusCode.isMarry, bean.isMarrage());
                            ToastUtil.showToast("修改成功");
                            mBaseIView.getContext().finish();
                        }
                    });
        }
    }

    private void checkBtn() {
        if (TextUtils.isEmpty(bean.getName())
                || TextUtils.isEmpty(bean.getSex())
                || TextUtils.isEmpty(bean.getBirthday())
                || TextUtils.isEmpty(bean.getIdCardNo())
                || TextUtils.isEmpty(bean.getMobile())
                || (bean.isMarrage() && TextUtils.isEmpty(bean.getSpouseName()))
                || (bean.isMarrage() && TextUtils.isEmpty(bean.getSpouseMobile()))
                || TextUtils.isEmpty(bean.getIncome())
                || TextUtils.isEmpty(bean.getIndustry())
                || TextUtils.isEmpty(bean.getAddress())
                || TextUtils.isEmpty(bean.getDetailAddress())) {
            //提交按钮不可点击
            canSubmit = false;
            mBaseIView.getSubmitView().setBackground(ContextCompat.getDrawable(mBaseIView.getContext(), R.drawable.shape_btn_unclick));
            return;
        }
        //按钮可点击
        canSubmit = true;
        mBaseIView.getSubmitView().setBackground(ContextCompat.getDrawable(mBaseIView.getContext(), R.drawable.shape_main_button));
    }

    private Map checkParams() {
        if (isNull(bean.getName(), "请输入姓名")) return null;
        if (isNull(bean.getSex(), "请选择性别")) return null;
        if (isNull(bean.getBirthday(), "请选择出生日期")) return null;
        if (isNull(bean.getIdCardNo(), "请输入身份证号")) return null;
        if (!Validator.isIDCard(bean.getIdCardNo())) {
            ToastUtil.showToast("身份号格式不正确");
            return null;
        }
        if (isNull(bean.getMobile(), "请输入联系方式")) return null;
        if (!Validator.isMobile(bean.getMobile())) {
            ToastUtil.showToast("请输入正确联系方式");
            return null;
        }
//        if (isNull(bean.getBankCardNo(), "请输入银行卡号")) return null;
        if (bean.isMarrage() && isNull(bean.getSpouseName(), "请输入配偶姓名")) return null;
        if (bean.isMarrage() && isNull(bean.getSpouseMobile(), "请输入配偶联系方式")) return null;
        if (bean.isMarrage() && !Validator.isMobile(bean.getSpouseMobile())) {
            ToastUtil.showToast("请输入正确配偶联系方式");
            return null;
        }
        if (isNull(bean.getIncome(), "请选择当请收入")) return null;
        if (isNull(bean.getIndustry(), "请选择行业")) return null;
        if (isNull(bean.getAddress(), "请选择常住地址")) return null;
        if (isNull(bean.getDetailAddress(), "请输入详细地址")) return null;
        return GsonUtil.toMap(bean);
    }

    private boolean isNull(String str, String toast) {
        if (TextUtils.isEmpty(str)) {
            ToastUtil.showToast(toast);
            return true;
        }
        return false;
    }
}
